package com.xlh.dto.courseTest.paper;

import com.google.common.base.Objects;
import com.xlh.dto.courseTest.AnswerSheetTypeDTO;
import com.xlh.enums.courseTest.QuestionTypeEnum;
import com.xlh.pojo.courseTest.CourseTestPaperAnswer;
import com.xlh.pojo.courseTest.CourseTestPaperAnswerSheet;
import com.xlh.pojo.courseTest.CourseTestPaperQuestion;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr 2019/4/18
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class BasePaperDTO extends StudentPaperDTO {
    // 题干
    private String content;
    // 选项信息
    private List<String> choiceList;
    // 是否为多选题
    private Boolean isMultipleChoice;
    // 学生当前题目填写的答案
    private Object answer;

    /**
     * 答题卡信息
     */
    // 全局答题卡列表
    List<AnswerSheetTypeDTO> answerSheetList;

    public BasePaperDTO transform(
            List<CourseTestPaperQuestion> questionList,
            CourseTestPaperQuestion question,
            List<CourseTestPaperAnswer> questionAnswerList,
            CourseTestPaperAnswerSheet answerSheet,
            List<AnswerSheetTypeDTO> answerSheetTypeList,
            Object userAnswer, Long timeLeft
    ) {
        // 查询选择题选项
        boolean isChoice = Objects.equal(question.getType(), QuestionTypeEnum.CHOICE.getCode());

        long rightAnswerCount = questionAnswerList.stream().filter(CourseTestPaperAnswer::getIsCorrect).count();

        this.setId(question.getId());

        this.setOrderNumber(question.getOrderNumber());
        this.setType(question.getType());
        this.setScore(question.getScore());
        this.setContent(question.getHtmlContent());
        this.setQuestionCount(questionList.size());
        this.setTypeName(QuestionTypeEnum.forCode(this.getType()).getMessage());
        this.setChoiceList(questionAnswerList.stream().map(CourseTestPaperAnswer::getContent).collect(Collectors.toList()));
        this.setIsMultipleChoice(isChoice ? rightAnswerCount > 1 : null);
        this.setAnswerSheetList(answerSheetTypeList);
        this.setAnswerSheetId(answerSheet.getId());
        this.setTimeLeft(timeLeft);
        this.setAnswer(userAnswer);
        return this;
    }
}
